package kpython.backend.ir.canon;

import kpython.backend.ir.tree.IRTreeVisitor;
import kpython.backend.ir.tree.Stmt;

public class PrintFunction extends Stmt {

	public StringBuilder code = new StringBuilder();
	
	public PrintFunction() {
		code.append("goto print_skip;\n");
		code.append("print:\n");		
				
		code.append("if(sp->locals[0].type == v_string)\n");
		code.append("\tprintf(\"%s\\n\", sp->locals[0].value.string);\n");
		code.append("if(sp->locals[0].type == v_boolean) {\n");
		code.append("if(sp->locals[0].value.boolean == 0)\n");
		code.append("printf(\"false\\n\");\n");
		code.append("else\n");
		code.append("printf(\"true\\n\");\n");
		code.append("}");
		code.append("if(sp->locals[0].type == v_integer)\n");		
		code.append("printf(\"%d\\n\", sp->locals[0].value.integer);\n");
		code.append("if(sp->locals[0].type == v_float)\n");	
		code.append("printf(\"%f\\n\", sp->locals[0].value.real);\n");						
		code.append("goto redirector;\n");
		code.append("print_skip:\n");
	}
	
	@Override
	public Object accept(IRTreeVisitor visitor, Object data) {
		return visitor.visit(this, data);
	}

	@Override
	public void printTree(String tab) {		
		System.out.println(tab+this.getClass().getSimpleName());
		tab = tab+"\t";
		System.out.println(tab+"Label print");		
	}
	
	public String toString() {
		return this.getClass().getSimpleName();
	}

}
